package windelpdv;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Marcio
 */
public class Principal extends javax.swing.JFrame {

    Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
    private Connection ConexaoWindel = null;
    private Connection ConexaoPDVWindel = null;

    /**
     * Creates new form frmPrincipal
     */
    public Principal() {
        initComponents();
        //setSize(600, 600); //ponha aqui suas medidas  
        setLocation((tela.width - this.getSize().width) / 2, (tela.height - this.getSize().height) / 2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        menuArquivo = new javax.swing.JMenu();
        menuPDV = new javax.swing.JMenuItem();
        menuFechaCaixa = new javax.swing.JMenuItem();
        menuAbriCaixa = new javax.swing.JMenuItem();
        menuRetiradaCaixa = new javax.swing.JMenuItem();
        menuRelatorios = new javax.swing.JMenu();
        menuVendas = new javax.swing.JMenu();
        menuVendasPeriodo = new javax.swing.JMenuItem();
        menuSair = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Principal");

        menuArquivo.setMnemonic('A');
        menuArquivo.setText("Arquivo");

        menuPDV.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        menuPDV.setMnemonic('P');
        menuPDV.setText("PDV Windel");
        menuPDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuPDVActionPerformed(evt);
            }
        });
        menuArquivo.add(menuPDV);

        menuFechaCaixa.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        menuFechaCaixa.setMnemonic('F');
        menuFechaCaixa.setText("Fechar Caixa");
        menuFechaCaixa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFechaCaixaActionPerformed(evt);
            }
        });
        menuArquivo.add(menuFechaCaixa);

        menuAbriCaixa.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        menuAbriCaixa.setMnemonic('A');
        menuAbriCaixa.setText("Abrir Caixa");
        menuAbriCaixa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAbriCaixaActionPerformed(evt);
            }
        });
        menuArquivo.add(menuAbriCaixa);

        menuRetiradaCaixa.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        menuRetiradaCaixa.setMnemonic('R');
        menuRetiradaCaixa.setText("Retirada Caixa");
        menuRetiradaCaixa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuRetiradaCaixaActionPerformed(evt);
            }
        });
        menuArquivo.add(menuRetiradaCaixa);

        jMenuBar1.add(menuArquivo);

        menuRelatorios.setMnemonic('R');
        menuRelatorios.setText("Relatórios");

        menuVendas.setText("Vendas");

        menuVendasPeriodo.setText("Por Período");
        menuVendasPeriodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuVendasPeriodoActionPerformed(evt);
            }
        });
        menuVendas.add(menuVendasPeriodo);

        menuRelatorios.add(menuVendas);

        jMenuBar1.add(menuRelatorios);

        menuSair.setText("Sair");
        menuSair.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menuSairMouseClicked(evt);
            }
        });
        menuSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSairActionPerformed(evt);
            }
        });
        jMenuBar1.add(menuSair);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuPDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPDVActionPerformed
        if (conectarWindel()) {
            if (conectarPDVWindel()) {
                frmPdv pdv = new frmPdv(ConexaoWindel, ConexaoPDVWindel);
                pdv.setVisible(true);
                pdv.setExtendedState(frmPdv.MAXIMIZED_BOTH);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar com o banco de dados!", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_menuPDVActionPerformed

    private void menuSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSairActionPerformed
        System.exit(0);
    }//GEN-LAST:event_menuSairActionPerformed

    private void menuFechaCaixaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFechaCaixaActionPerformed
        if (conectarPDVWindel()) {
            frmFechaCaixa fc = new frmFechaCaixa(ConexaoPDVWindel);
            fc.setVisible(true);
        }
    }//GEN-LAST:event_menuFechaCaixaActionPerformed

    private void menuAbriCaixaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAbriCaixaActionPerformed
       if (conectarPDVWindel()) {
            IniciaCaixa fc = new IniciaCaixa(ConexaoPDVWindel);
            fc.setVisible(true);
        }
    }//GEN-LAST:event_menuAbriCaixaActionPerformed

    private void menuRetiradaCaixaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuRetiradaCaixaActionPerformed
        if (conectarPDVWindel()) {
            frmRetiradaCaixa fc = new frmRetiradaCaixa(ConexaoPDVWindel);
            fc.setVisible(true);
        }
    }//GEN-LAST:event_menuRetiradaCaixaActionPerformed

    private void menuSairMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuSairMouseClicked
            System.exit(0);
    }//GEN-LAST:event_menuSairMouseClicked

    private void menuVendasPeriodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuVendasPeriodoActionPerformed
        URL arquivo = getClass().getResource("rpVendas.jasper");
        
        
        
        //rpVendas vendas = new rpVendas();
    }//GEN-LAST:event_menuVendasPeriodoActionPerformed

    public boolean conectarPDVWindel() {
        String LocalBancoDados = "jdbc:firebirdsql:192.168.254.7:C:/Marcio/Banco/WindelPDVTeste.fdb";        
        String Usuario = "SYSDBA";
        String Senha = "masterkey";
        String NomeDriver = "org.firebirdsql.jdbc.FBDriver";
        try {
            Class.forName(NomeDriver).newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        try {
            ConexaoPDVWindel = DriverManager.getConnection(LocalBancoDados, Usuario, Senha);
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean conectarWindel() {
        String LocalBancoDados = "jdbc:firebirdsql:192.168.254.7:C:/WINDEL/dados/sistema.fdb";
        String Usuario = "SYSDBA";
        String Senha = "masterkey";
        String NomeDriver = "org.firebirdsql.jdbc.FBDriver";
        try {
            Class.forName(NomeDriver).newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        try {
            ConexaoWindel = DriverManager.getConnection(LocalBancoDados, Usuario, Senha);
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem menuAbriCaixa;
    private javax.swing.JMenu menuArquivo;
    private javax.swing.JMenuItem menuFechaCaixa;
    private javax.swing.JMenuItem menuPDV;
    private javax.swing.JMenu menuRelatorios;
    private javax.swing.JMenuItem menuRetiradaCaixa;
    private javax.swing.JMenu menuSair;
    private javax.swing.JMenu menuVendas;
    private javax.swing.JMenuItem menuVendasPeriodo;
    // End of variables declaration//GEN-END:variables
}
